import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
import * as path from 'path';
import {redisPwd,ipAddree} from './constant';

export default (appInfo: EggAppInfo) => {
    const config = {} as PowerPartial<EggAppConfig>;


    config.keys = appInfo.name + '_1566960005072_482';

    // 中间件
    config.middleware = [ 'authority', 'logs' , 'exception' ];

    config.jwt = {
        secret: 'JKFBBBAIKUH',
    };



    config.security = {
        csrf: {
            enable: false,
        },
        domainWhiteList:['*']
    }

    // //多出来的配置==========
    config.cors = {
        credentials: true, // 允许跨域请求携带cookies
        origin: ctx => ctx.get('origin'),
        // origin: ctx => ctx.get('origin'),
        // origin: () => 'http://qianshop.fortunemy.top',//这边不能为*号，需要指定明确的、与请求网页一致的域名,
        // origin:'http://localhost:9000,http://qianshop.fortunemy.top,http://127.0.0.1:7001',
        allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS'
    }


    const whitelist = [
        // images
        '.jpg', '.jpeg', // image/jpeg
        '.png', // image/png, image/x-png
        '.gif', // image/gif
        '.bmp', // image/bmp
        '.wbmp', // image/vnd.wap.wbmp
        '.webp',
        '.tif',
        '.psd',
        // text
        '.svg',
        '.js', '.jsx',
        '.json',
        '.css', '.less',
        '.html', '.htm',
        '.xml',
        // tar
        '.zip',
        '.gz', '.tgz', '.gzip',
        // video
        '.mp3',
        '.mp4',
        '.avi',
        '.xlsx',
        '.xls',
    ];

    config.multipart = {
        fileSize: '100mb',
        // mode: 'file',
        whitelist,
        mode: 'stream',
        // fileModeMatch: /^\/upload$/,//    upload接口使用file模式，其他使用stream模式
        // fileModeMatch: /^(\/tools\/excel\/import)$/, // '/tools/excel/import'接口使用file模式，其他使用stream模式
        // fileExtensions: ['.xlsx', '.xls'],
    };

    config.redis = {
        client: {
            port: 6379,
            host : ipAddree,
            password: redisPwd,
            db: 0,
        },
    };


    config.oss = {
        client: {
            accessKeyId: '',
            accessKeySecret: '',
            bucket: '',
            endpoint: '',
            timeout: '3600s',
        },
    };



    config.view = {
        root: [
            path.join(appInfo.baseDir, 'app/view'),
        ].join(','),
        defaultViewEngine: 'nunjucks',
        defaultExtension: '.html',
        mapping: {
            '.html': 'nunjucks',
        },
    };

    config.sex = {
        man:1,
        woman:2,
        both:3
    }

    // 靜態目錄及緩存設置
    config.static = {
        prefix: '/',
        dir: path.join(appInfo.baseDir, 'app/public'),
        dynamic: true,//是否緩存靜態資源
        preload: false,//啓動項目開啓緩存
        // maxAge: 31536000,
        maxAge: 0,//緩存時間 開發建議設0 跳坑
        buffer: true,//是否緩存到内存 默認prod 緩存
    };


    config.wxApp = {
        // tokenUrl:"https://api.weixin.qq.com/cgi-bin/token",
        appId:'',
        appSecret:'',
        name:'倩优卓小程序',
        accessTokenRedisKey:'qianyouzhuo_accesstoken',
        qrCodeWidth:300
    }

    config.wxApi = {
        accessTokenUrl : 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET',
        createQRCodeUrl:'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN',
    }

    config.wxAuthMobiles = [

    ]

    config.pictureSplit = {
        logo:'-logo.webp',
        qianyouzhuo:'-qianyouzhuo.webp',
        shipin:'-shipin.jpg',
        banner:'-banner.webp',
        detail:'-detail.webp'
    }

    // 新增特殊的业务配置
    const bizConfig = {
        token: {
            expires: 24 * 3600 * 7, // token 过期时间
        },
        SSO: false, // 单点登录
        upload: 'oss', // 可选 local：存储在本地； oss：存储在阿里云的oss
        charComma:'ε',
        charSPLIT:'η',
        timeCondition:{  //时间统计常亮
            THREE_MONTH:1,
            ONE_MONTH:2,
            TWO_WEEK:3,
            ONE_WEEK:4,
            THREE_DAY:5,
            ONE_DAY:6,
        }
    };


    return {
        ...config,
        ...bizConfig
    };
};
